package priv.pfz.basedao.jdbctemplate.sqlprovider;

import com.google.common.collect.Lists;
import priv.pfz.basedao.info.DaoInfo;
import priv.pfz.basedao.jdbctemplate.SqlResult;
import priv.pfz.basedao.sharding.ShardingInfo;
import priv.pfz.basedao.sharding.ShardingManager;
import priv.pfz.basedao.sqlbuilder.SQL;

import java.util.List;

/**
 * DELETE FROM table WHERE id=#{id}
 * logic delete: UPDATE table SET delete_flag = id WHERE id=#{id} AND delete_flag=0
 * @author yyjxp
 * 2024/1/28 23:55
 */
public class DeleteByIdSqlProvider extends BaseSqlProvider {

    public static SqlResult provideSql(long id, ShardingInfo shardingInfo, DaoInfo daoInfo) {
        ShardingManager shardingManager = new ShardingManager(daoInfo, shardingInfo);
        List<Object> argList = Lists.newArrayList();

        SQL sql = new SQL();
        sql.WHERE(getIdEquals());
        argList.add(id);
        if (daoInfo.isEnableLogicDelete()) {
            sql.UPDATE(shardingManager.getShardingTable());
            sql.SET(getLogicDeleteSet(daoInfo));
            sql.WHERE(getLogicDeleteWhere(daoInfo));
        } else {
            sql.DELETE_FROM(shardingManager.getShardingTable());
        }
        String sqlStr = sql.toString();
        sqlStr += shardingManager.getThirdShardingStr();

        System.out.println(sqlStr);
        System.out.println(argList);
        return new SqlResult(sqlStr, argList);
    }
}
